using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.WebControls
{
    public class AcmsDataListItem : WebControl, IDataItemContainer, INamingContainer
    {
        public AcmsDataListItem():base("div")
        {
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        private AcmsDataListItem(bool isHeader, bool isFooter, bool isSeperator, bool isTopPager, bool isBottomPager, bool isNoData, bool isItem, bool isAlternateItem, bool isSelectedItem, bool isEditItem, bool isInsertItem, int itemIndex, int currentPage, int pageSize)
        {
            this.isHeader = isHeader;
            this.isFooter = isFooter;
            this.isSeperator = isSeperator;
            this.isTopPager = isTopPager;
            this.isBottomPager = isBottomPager;
            this.isNoData = isNoData;
            this.isItem = isItem;
            this.isAlternateItem = isAlternateItem;
            this.isSelectedItem = isSelectedItem;
            this.isEditItem = isEditItem;
            this.isInsertItem = isInsertItem;
            this.itemIndex = itemIndex;
        }

        internal static AcmsDataListItem CreateHeader()
        {
            return new AcmsDataListItem(true, false, false, false, false, false, false, false, false, false, false, -1, 0, 0);
        }

        internal static AcmsDataListItem CreateFooter()
        {
            return new AcmsDataListItem(false, true, false, false, false, false, false, false, false, false, false, -1, 0, 0);
        }

        internal static AcmsDataListItem CreateSeperator(int index)
        {
            return new AcmsDataListItem(false, false, true, false, false, false, false, false, false, false, false, index, 0, 0);
        }

        internal static AcmsDataListItem CreatePager(bool isTop)
        {
            return new AcmsDataListItem(false, false, false, isTop, !isTop, false, false, false, false, false, false, -1, 0, 0);
        }

        internal static AcmsDataListItem CreateItem(int index)
        {
            return new AcmsDataListItem(false, false, false, false, false, false, index % 2 == 0, index % 2 != 0, false, false, false, index, 0, 0);
        }

        internal static AcmsDataListItem CreateSelectedItem(int index)
        {
            return new AcmsDataListItem(false, false, false, false, false, false, index % 2 == 0, index % 2 != 0, true, false, false, index, 0, 0);
        }

        internal static AcmsDataListItem CreateEditItem(int index)
        {
            return new AcmsDataListItem(false, false, false, false, false, false, index % 2 == 0, index % 2 != 0, false, true, false, index, 0, 0);
        }

        internal static AcmsDataListItem CreateInsertItem(int index)
        {
            return new AcmsDataListItem(false, false, false, false, false, false, index % 2 == 0, index % 2 != 0, false, false, true, index, 0, 0);
        }

        internal static AcmsDataListItem CreateNoData()
        {
            return new AcmsDataListItem(false, false, false, false, false, true, false, false, false, false, true, -1, 0, 0);
        }

        private bool isHeader;
        public bool IsHeader
        {
            get { return isHeader; }
        }

        private bool isFooter;
        public bool IsFooter
        {
            get { return isFooter; }
        }

        private bool isSeperator;
        public bool IsSeperator
        {
            get { return isSeperator; }
        }

        private bool isTopPager;
        public bool IsTopPager
        {
            get { return isTopPager; }
            set { isTopPager = value; }
        }

        private bool isBottomPager;
        public bool IsBottomPager
        {
            get { return isBottomPager; }
            set { isBottomPager = value; }
        }

        public bool IsPager
        {
            get { return isTopPager || isBottomPager; }
        }

        private bool isNoData;
        public bool IsNoData
        {
            get { return isNoData; }
        }

        private bool isItem;
        public bool IsItem
        {
            get { return isItem; }
        }

        private bool isAlternateItem;
        public bool IsAlternateItem
        {
            get { return isAlternateItem; }
        }

        private bool isSelectedItem;
        public bool IsSelectedItem
        {
            get { return isSelectedItem; }
        }

        private bool isEditItem;
        public bool IsEditItem
        {
            get { return isEditItem; }
        }

        private bool isInsertItem;
        public bool IsInsertItem
        {
            get { return isInsertItem; }
        }

        public bool IsDataItem
        {
            get { return isItem || isAlternateItem || isSelectedItem || isEditItem || isInsertItem; }
        }

        public bool IsNormalItem
        {
            get { return !isSelectedItem && (isItem || isAlternateItem); }
        }

        private int itemIndex = -1;
        public int ItemIndex
        {
            get{return itemIndex;}
        }

        private int currentPage;
        public int CurrentPage
        {
            get { return currentPage; }
        }

        private int pageSize;
        public int PageSize
        {
            get { return pageSize; }
        }
	
	
        private object dataItem;
        internal void SetDataSource(object dataSource)
        {
            this.dataItem = dataSource;
        }

        #region IDataItemContainer Members

        public object DataItem
        {
            get { return dataItem; }
        }

        public int DataItemIndex
        {
            get { return (pageSize * currentPage) + itemIndex; }
        }

        public int DisplayIndex
        {
            get { return itemIndex; }
        }

        #endregion

        public new void ApplyStyle(Style style)
        {
            if (style != null)
            {
                base.ApplyStyle(style);
                if (!string.IsNullOrEmpty(style.CssClass))
                {
                    this.CssClass = style.CssClass;
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (isSelectedItem)
            {
                Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "isSelected", isSelectedItem.ToString().ToLower());
            }
            if (IsPager)
            {
                AcmsDataList list = this.NamingContainer as AcmsDataList;
                if (list != null && list.TotalRecords <= list.PageSize)
                {
                    this.Visible = false;
                }
            }
            base.OnPreRender(e);
        }

        protected override object SaveViewState()
        {
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            if (args is CommandEventArgs)
            {
                CommandEventArgs command = (CommandEventArgs)args;
                AcmsDataListItemCommandEventArgs e = new AcmsDataListItemCommandEventArgs(this, command.CommandName, command.CommandArgument);
                base.RaiseBubbleEvent(this, e);
                return true;
            }
            else
                return false;
        }

        public UpdatePanel AjaxPanel
        {
            get 
            {
                if (this.Parent != null && this.Parent.Parent != null)
                    return this.Parent.Parent as UpdatePanel;
                else
                    return null;
            }
        }
    }
}
